﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;

namespace TEAA
{
    public class DBParser
    {

        public static IDictionary<CommandType, List<string>> ParseQuery(string query)
        {
            IDictionary<CommandType, List<string>> parsedText = new Dictionary<CommandType, List<string>>();

            string tempQuery = query.ToLower();
            Regex regex;
            foreach (CommandType command in Enum.GetValues(typeof(CommandType)))
            {
                regex = new Regex(StringEnum.GetStringValue(command));
                string commandString = regex.Match(tempQuery).Value;
                if (commandString.Length > 0)
                {
                    tempQuery = tempQuery.Replace(commandString, string.Empty);
                    List<string> parameters = new List<string>();
                    commandString = commandString.Replace(command.ToString(), "");
                    commandString = commandString.Replace(" ", string.Empty);
                    string[] parameterStrings = commandString.Split(new char[] {','});
                    foreach (string parameter in parameterStrings)
                        parameters.Add(parameter);
                    parsedText.Add(new KeyValuePair<CommandType, List<string>>(command, parameters));
                }
            }

            if (!tempQuery.Trim().Equals(string.Empty))
                throw new Exception("Query syntax error!");

            return parsedText;

        }

    }
}
